package com.seashop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seashop.entity.DataDictionary;
import com.seashop.enums.LanguageTypeEnum;
import com.seashop.mapper.DataDictionaryMapper;
import com.seashop.request.LocalTransRequestParam;
import com.seashop.service.IDataDictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 翻译词条表 服务实现类
 * </p>
 *
 * @author moredln
 * @since 2020-08-11
 */
@Service
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryMapper, DataDictionary> implements IDataDictionaryService {

    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    @Override
    public String localTrans(LocalTransRequestParam param) {
        String text = param.getText();
        String fromLanguageType = param.getFromLanguage();
        String toLanguageType = param.getToLanguage();
        String result = "";
        QueryWrapper<DataDictionary> queryWrapper = new QueryWrapper<>();
        if (LanguageTypeEnum.EN.getName().equals(fromLanguageType)) {
            queryWrapper.eq("name", text);
        } else if (LanguageTypeEnum.ZH.getName().equals(fromLanguageType)) {
            queryWrapper.eq("name_zh", text);
        } else if (LanguageTypeEnum.ID.getName().equals(fromLanguageType)) {
            queryWrapper.eq("name_id", text);
        } else if (LanguageTypeEnum.TH.getName().equals(fromLanguageType)) {
            queryWrapper.eq("name_th", text);
        } else if (LanguageTypeEnum.UR.getName().equals(fromLanguageType)) {
            queryWrapper.eq("name_ur", text);
        }
        queryWrapper.eq("is_delete", false);
        List<DataDictionary> dataDictionaries = dataDictionaryMapper.selectList(queryWrapper.select("name_th", "name_id", "name_ur", "name_zh", "name_ur"));
        if (!dataDictionaries.isEmpty()) {
            if (toLanguageType.equals(LanguageTypeEnum.TH.getName())) {
                result = dataDictionaries.get(0).getNameTh();
            } else if (toLanguageType.equals(LanguageTypeEnum.ID.getName())) {
                result = dataDictionaries.get(0).getNameId();
            } else if (toLanguageType.equals(LanguageTypeEnum.ZH.getName())) {
                result = dataDictionaries.get(0).getNameZh();
            } else if (toLanguageType.equals(LanguageTypeEnum.UR.getName())) {
                result = dataDictionaries.get(0).getNameUr();
            }
        }
        return result;
    }
}
